package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;
import com.ecoolex.ying.space.core.dao.EstateProjectActivityHouseModelItemsGoodsDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelItemsGoods.Column;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelItemsGoodsExample.Criteria;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityHouseModelItemImagesDTO;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityHouseModelItemsGoodsDTO;import com.sun.mail.handlers.image_gif;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelItemsGoods;
import com.ecoolex.ying.space.entity.EstateProjectActivityHouseModelItemsGoodsExample;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityHouseModelItemsGoodsManager {
	
    @Autowired
    private EstateProjectActivityHouseModelItemsGoodsDAO dao;
    
    public void edit(Integer itemId,AddEstateProjectActivityHouseModelItemImagesDTO imagesDTO,String updateUser) {
    	List<EstateProjectActivityHouseModelItemsGoods> oldList = listByStorageId(imagesDTO.getStorageId());
    	List<AddEstateProjectActivityHouseModelItemsGoodsDTO> addModelItemsGoodsList = imagesDTO.getAddModelItemsGoodsList();
    	if(Check.notNullOrEmpty(addModelItemsGoodsList)) {
    		for (AddEstateProjectActivityHouseModelItemsGoodsDTO itemsGoodsDTO : addModelItemsGoodsList) {
    			//不存在则新增
    			if(Check.isNull(itemsGoodsDTO.getId())) {
    				EstateProjectActivityHouseModelItemsGoods record = new EstateProjectActivityHouseModelItemsGoods();
    				record.setItemId(itemId);
    				record.setActivityGoodsId(itemsGoodsDTO.getActivityGoodsId());
    				record.setLabel(itemsGoodsDTO.getLabel());
    				record.setStorageId(imagesDTO.getStorageId());
    				record.setDeleted(EstateProjectActivityHouseModelItemsGoods.NOT_DELETED);
    				record.setCreateUser(updateUser);
    				record.setCreateTime(Date.from(Instant.now()));
    				record.setUpdateUser(updateUser);
        			record.setUpdateTime(Date.from(Instant.now()));
            		dao.insert(record);
    			}else{
    				//编辑
    				EstateProjectActivityHouseModelItemsGoods old = oldList.stream().collect(CollectorUtil.first(o -> itemsGoodsDTO.getActivityGoodsId().equals(o.getActivityGoodsId())));
    				old.setActivityGoodsId(itemsGoodsDTO.getActivityGoodsId());
    				old.setLabel(itemsGoodsDTO.getLabel());
    				old.setUpdateUser(updateUser);
                    old.setUpdateTime(Date.from(Instant.now()));
                    dao.updateByPrimaryKeySelective(old,
                        Column.excludes(Column.deleted, Column.createTime, Column.createUser));
    				oldList.remove(old);
    			}
    		}
    		oldList.forEach(old -> delete(old,updateUser));
    	}else {
    		//传进来空的
    		oldList.forEach(old -> delete(old,updateUser));
    	}
    }
    
    public List<EstateProjectActivityHouseModelItemsGoods> listByItemId(Integer itemId){
    	EstateProjectActivityHouseModelItemsGoodsExample example
			= new EstateProjectActivityHouseModelItemsGoodsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andItemIdEqualTo(itemId).andDeletedEqualTo(EstateProjectActivityHouseModelItemsGoods.NOT_DELETED);
    	return dao.selectByExample(example);
    }
    
    public List<EstateProjectActivityHouseModelItemsGoods> listByStorageId(String storageId){
    	EstateProjectActivityHouseModelItemsGoodsExample example
			= new EstateProjectActivityHouseModelItemsGoodsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andStorageIdEqualTo(storageId).andDeletedEqualTo(EstateProjectActivityHouseModelItemsGoods.NOT_DELETED);
    	return dao.selectByExample(example);
    }
    
    public List<EstateProjectActivityHouseModelItemsGoods> listByActivityGoodsId(Integer activityGoodsId){
    	EstateProjectActivityHouseModelItemsGoodsExample example
			= new EstateProjectActivityHouseModelItemsGoodsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andActivityGoodsIdEqualTo(activityGoodsId).andDeletedEqualTo(EstateProjectActivityHouseModelItemsGoods.NOT_DELETED);
    	return dao.selectByExample(example);
    } 
    
    public void deleteByActivityGoodsId(Integer activityGoodsId,String updateUser) {
    	List<EstateProjectActivityHouseModelItemsGoods> list = listByActivityGoodsId(activityGoodsId);
    	list.forEach(itemGoods -> {
    		delete(itemGoods,updateUser);
    	});
    }
    
    public void delete(EstateProjectActivityHouseModelItemsGoods oldModelGoods,String updateUser) {
    	oldModelGoods.setDeleted(EstateProjectActivityHouseModelItemsGoods.IS_DELETED);
    	oldModelGoods.setUpdateUser(updateUser);
    	oldModelGoods.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(oldModelGoods, Column.deleted,Column.updateUser,Column.updateTime);
    }
    
    public void deleteByStorageId(String storageId,String updateUser) {
    	List<EstateProjectActivityHouseModelItemsGoods> list = listByStorageId(storageId);
		list.forEach(itemsGoods -> {
			delete(itemsGoods,updateUser);
		});	
		
    }
}